<?php
namespace Home\Controller;

use Think\Controller;

class AddressController extends BaseController
{
    /*进入主页面*/
    public function index()
    {
        $id = I('id');
        $member_id = parent::get("account")['id'];
        if (empty($member_id)) {
            $this->ajaxError("未登录");
        }
        if (empty($id)) {
            //获取所有收货地址
            $address = array();
            $where_all['memberId'] = $member_id;
            $where_all['isDelete'] = 2;
            $address_list = M('address')
                ->alias("a")
                ->join("LEFT JOIN t_region r on r.id = a.regionId")
                ->field("a.*,r.name_path")
                ->where($where_all)->select();
            return $this->ajaxSuccess(array("province" => getCity(0), "address" => $address, "address_list" => $address_list));
        } else {
            //获取要编辑的收货地址
            $id = I('id');
            $where['a.memberId'] = $member_id;
            $where['a.id'] = $id;
            $where['a.isDelete'] = 2;
            $address = M('address')
                ->alias('a')
                ->join("LEFT JOIN t_region r on r.id = a.regionId")
                ->field("a.*,r.id_path")
                ->where($where)
                ->order('isdefault')
                ->find();
            $addres_arr = $arr = explode(",", $address['id_path']);
            $city_id = $addres_arr[1];
            $area_id = $addres_arr[2];
            $address['province_id'] = $addres_arr[0];
            $address['city_id'] = $city_id;
            $address['area_id'] = $area_id;
            $where_all['memberId'] = $member_id;
            $where_all['isDelete'] = 2;
            $address_list = M('address')
                ->where($where_all)
//                ->join("LEFT JOIN Region on Region.id = MemberAddress.regionId")
                ->order('isdefault')
                ->select();
            return $this->ajaxSuccess(array("city_id" => $city_id, "area_id" => $area_id, "city" => getCity($addres_arr[0]), "area" => getCity($city_id), "province" => getCity(0), "address" => $address, "address_list" => $address_list));
        }
    }

    // 添加、编辑地址
    public function save()
    {
        if (IS_POST) {
            $account_id = parent::get("account")['id'];

            if (empty($account_id)) {
                $this->ajaxError("未登录");
            }
            $phone = I("phone");
            $name = I("name");
            $regionId = I("xian");
            if (empty($regionId)) {
                $xianaddress = I("xianaddress");
                $regionaddress = explode("/", trim($xianaddress));
                $prov = mb_substr($regionaddress[0], 0, 2, 'utf-8');
                $city = mb_substr($regionaddress[1], 0, 2, 'utf-8');
                $area = mb_substr($regionaddress[2], 0, 2, 'utf-8');
                $region = M('region')
                    ->where("`name_path` LIKE '$prov%' AND `name_path` LIKE '%$city%' AND `name_path` LIKE '%$area%'")
                    ->find();
                $regionId = $region['id'];
            }
            $newaddress = I("post.newaddress");
            if (empty($regionId) || $regionId == 0) {
                return $this->ajaxError("请选择所在区域");
            }

            if (empty($newaddress)) {
                return $this->ajaxError("详细地址不能为空");
            }
            if (empty($name)) {
                return $this->ajaxError("名字不能为空");
            }
            if (!isPhone($phone)) {
                return $this->ajaxError("手机号格式不正确");
            }
            $param = array(
                'phone' => $phone,
                'name' => $name,
                'regionId' => $regionId,
                'address' => $newaddress
            );

            if (!empty($_POST['id'])) {
                //编辑
                $id = $_POST['id'];
                $where['memberId'] = $account_id;
                $where['id'] = $id;
                $res = M('address')->where($where)->setField($param);
                // 判断返回值
                if (!empty($res)) {
                    $this->ajaxSuccess("修改地址成功!");
                } else {
                    $this->ajaxError("未修改地址或修改失败!");
                }
            } else {
                //添加
                $param['isDelete'] = 2;
                $param['memberId'] = $account_id;
                $param['isDefault'] = 2;
                $res = M('address')->add($param);
                // 判断返回值
                if (!empty($res)) {
                    $this->ajaxSuccess("添加地址成功!");
                } else {
                    $this->ajaxError("添加地址失败!");
                }
            }
        }
    }

    // 删除地址
    public function remove()
    {
        $account_id = parent::get("account")['id'];

        if (empty($account_id)) {
            $this->ajaxError("未登录");
        }
        $where['memberid'] = $account_id;
        $where['id'] = I('id');
        $res = M('address')->where($where)->save(array('isDelete' => 1));
        // 判断返回值
        if (!empty($res)) {
            $this->ajaxSuccess("删除地址成功!");
        } else {
            $this->ajaxError("删除地址失败!");
        }
    }

    // 设置默认地址
    public function setDefault()
    {
        $account_id = parent::get("account")['id'];

        if (empty($account_id)) {
            $this->ajaxError("未登录");
        }
        $where['memberId'] = $account_id;
        $where['id'] = I('id');

        M('address')->where(array('memberId' => $account_id))->setField(array('isDefault' => 2));
        $res = M('address')->where($where)->setField('isDefault', 1);
        // 判断返回值
        if (!empty($res)) {
            $this->ajaxSuccess("设置默认地址成功！");
        } else {
            $this->ajaxError("设置默认地址失败!");
        }
    }

    // 代理地址
    public function agenindex()
    {
        $id = I('id');
        $agenid = parent::get("agen")['id'];

        if (empty($agenid)) {
            $this->ajaxError("未登录");
        }
        if (empty($id)) {
            $address = array();
            $where_all['agenId'] = $agenid;
            $where_all['isDelete'] = 2;
            $address_list = M('agenaddress')
                ->join("LEFT JOIN region on region.id = agenaddress.regionId")
                ->field("agenaddress.*,region.name_path")
                ->where($where_all)->select();
            return $this->ajaxSuccess(array("province" => getCity(0), "address" => $address, "address_list" => $address_list));
        } else {
            $id = I('id');
            $where['a.agenId'] = $agenid;
            $where['a.id'] = $id;
            $where['a.isDelete'] = 2;
            $address = M('agenaddress')
                ->alias('a')
                ->join("LEFT JOIN Region on Region.id = a.regionId")
                ->field("a.*,Region.id_path")
                ->where($where)
                ->find();
            $addres_arr = $arr = explode(",", $address['id_path']);
            $city_id = $addres_arr[1];
            $area_id = $addres_arr[2];
            $address['province_id'] = $addres_arr[0];
            $address['city_id'] = $city_id;
            $address['area_id'] = $area_id;
            $where_all['agenId'] = $agenid;
            $where_all['isDelete'] = 2;
            $address_list = M('agenaddress')
                ->where($where_all)
//                ->join("LEFT JOIN Region on Region.id = MemberAddress.regionId")
                ->select();
            return $this->ajaxSuccess(array("city_id" => $city_id, "area_id" => $area_id, "city" => getCity($addres_arr[0]), "area" => getCity($city_id), "province" => getCity(0), "address" => $address, "address_list" => $address_list));
        }
    }

    // 保存代理地址
    public function agensave()
    {
        if (IS_POST) {
            $agenid = parent::get("agen")['id'];

            if (empty($agenid)) {
                $this->ajaxError("未登录");
            }
            $phone = I("post.phone");
            $name = I("post.name");
            $regionId = I("post.xian");
            if (empty($regionId)) {
                $xianaddress = I("post.xianaddress");
                $regionaddress = explode("/", trim($xianaddress));
                $prov = mb_substr($regionaddress[0], 0, 2, 'utf-8');
                $city = mb_substr($regionaddress[1], 0, 2, 'utf-8');
                $area = mb_substr($regionaddress[2], 0, 2, 'utf-8');
                $region = M('region')
                    ->where("`name_path` LIKE '$prov%' AND `name_path` LIKE '%$city%' AND `name_path` LIKE '%$area%'")
                    ->find();
                $regionId = $region['id'];
            }
            $newaddress = I("post.newaddress");

            if (empty($regionId) || $regionId == 0) {
                return $this->ajaxError("请选择所在区域");
            }

            if (empty($newaddress)) {
                return $this->ajaxError("详细地址不能为空");
            }
            if (empty($name)) {
                return $this->ajaxError("名字不能为空");
            }
            if (!isPhone($phone)) {
                return $this->ajaxError("手机号格式不正确");
            }
            $param = array(
                'phone' => $phone,
                'name' => $name,
                'regionId' => $regionId,
                'address' => $newaddress
            );

            if (!empty($_POST['id'])) {
                $id = $_POST['id'];

                $where['agenId'] = $agenid;
                $where['id'] = $id;
                $res = M('agenaddress')->where($where)->setField($param);
                // 判断返回值
                if (!empty($res)) {
                    $this->ajaxSuccess("修改地址成功!");
                } else {
                    $this->ajaxError("未修改地址或修改失败!");
                }
            } else {
                $param['isDelete'] = 2;
                $param['agenId'] = $agenid;
                $param['isDefault'] = 2;
                $res = M('agenaddress')->add($param);
                // 判断返回值
                if (!empty($res)) {
                    $this->ajaxSuccess("添加地址成功!");
                } else {
                    $this->ajaxError("添加地址失败!");
                }
            }
        }
    }

    // 删除代理地址
    public function agenremove()
    {
        $agenid = parent::get("agen")['id'];

        if (empty($agenid)) {
            $this->ajaxError("未登录");
        }
        $where['agenId'] = $agenid;
        $where['id'] = I('id');
        $res = M('agenaddress')->where($where)->save(array('isDelete' => 1));
        // 判断返回值
        if (!empty($res)) {
            $this->ajaxSuccess("删除地址成功!");
        } else {
            $this->ajaxError("删除地址失败!");
        }
    }

    // 设置代理默认地址
    public function agensetDefault()
    {
        $agenid = parent::get("agen")['id'];

        if (empty($agenid)) {
            $this->ajaxError("未登录");
        }
        $where['agenId'] = $agenid;
        $where['id'] = I('id');

        M('agenaddress')->where(array('agenId' => $agenid))->setField(array('isDefault' => 2));
        $res = M('agenaddress')->where($where)->setField('isDefault', 1);
        // 判断返回值
        if (!empty($res)) {
            $this->ajaxSuccess("设置默认地址成功！");
        } else {
            $this->ajaxError("设置默认地址失败!");
        }
    }
}
